/*
 * File Name:               UIContext.cs
 *
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>
 * Create Date:             2016/11/22
 */

using UnityEngine;

/// <summary>
/// UI上下文(用于获取UGUI组件) 
/// </summary>
[DisallowMultipleComponent]
public class UIContext : MonoBehaviour
{
    /// <summary>
    /// 单个组件的数据结构 
    /// </summary>
    [System.Serializable]
    public class Data
    {
        /// <summary>
        /// 子组件 
        /// </summary>
        public Transform pComponent;

        /// <summary>
        /// 子组件描述 
        /// </summary>
        public string pDescription = "子节点功能描述";
    }

    /// <summary>
    /// 子组件 
    /// </summary>
    public Data[] pComponents;

    /// <summary>
    /// 获取子组件 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public T getComponent<T>(int id = -1) where T : Component
    {
        if (id < 0)
        {
            return GetComponent<T>();
        }

        // 这里不做其他异常类判断是为了强制保证资源正确性
        if (pComponents == null)
            return null;

        return pComponents[id].pComponent.GetComponent<T>();
    }

    public void showByScale()
    {
        //gameObject.SetActive(true);
        // transform.localScale = Vector3.one;

        var trans = transform;
        var pos = trans.localPosition;
        pos.z = 0f;
        trans.localPosition = pos;
    }

    public void hideByScale()
    {
        //gameObject.SetActive(false);
        // transform.localScale = Vector3.zero;

        var trans = transform;
        var pos = trans.localPosition;
        pos.z = 100000f;
        trans.localPosition = pos;
    }
}